package com.zhangtai.modules.service.impl.transfer;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.TransferInfoDao;

import com.zhangtai.modules.entity.transfer.TransferInfoEntity;

import com.zhangtai.modules.service.transfer.TransferInfoService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class TransferInfoServiceImpl extends ServiceImpl<TransferInfoDao, TransferInfoEntity> implements TransferInfoService {

    @Autowired
    private TransferInfoDao transferInfoDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "isDistribute", required = false) Integer isDistribute,
                                @RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value="orgIds") List<Long> orgIds,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
                                ) {
        QueryWrapper<TransferInfoEntity> queryWrapper = new QueryWrapper<TransferInfoEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                .eq(isDistribute!=null,"is_distribute",isDistribute)
                .eq("is_delete",0).in(!ObjectUtils.isEmpty(orgIds),"unit_id",orgIds).
                        eq(ObjectUtils.isEmpty(orgIds),"unit_id",null).orderByDesc("create_time");

        IPage<TransferInfoEntity> page = this.page(
                new Page<TransferInfoEntity>(pageNum,pageSize),
                queryWrapper
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<TransferInfoEntity> queryPage(@RequestParam(value ="transferType")Integer transferType,
                                                    @RequestParam(value ="unitname")String unitname,
                                                    @RequestParam(value ="orgname")String orgname,
                                                    @RequestParam(value ="userCode")String userCode,
                                                    @RequestParam(value ="name")String name,
                                                    @RequestParam(value ="type")Integer type,
                                                    @RequestParam(value ="dealStatus")Integer dealStatus,
                                                    @RequestParam(value ="jobIds")List<Long> jobIds,
                                                    @RequestParam(value ="jobSystemIds")List<Long> jobSystemIds,
                                                    @RequestParam(value ="orgIds")List<Long> orgIds,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
                                ) {
        Page<TransferInfoEntity> page = new Page<TransferInfoEntity>(pageNum,pageSize);
        List<TransferInfoEntity> list = transferInfoDao.getTransferList(transferType,unitname,orgname,userCode,StringUtils.isEmpty(name)?null:name,type,dealStatus, jobIds,jobSystemIds,orgIds,  page);
        page.setRecords(list);
        return new PageResult(page);
    }


    @Override
    public TransferInfoEntity getByIdE(Long id) {
            TransferInfoEntity one = this.getOne(new QueryWrapper<TransferInfoEntity>().eq("transfer_id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(TransferInfoEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(TransferInfoEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            TransferInfoEntity entity = new TransferInfoEntity();
        entity.setTransferId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}